﻿using MediatR;
using SCM.API.Write.Application.Command.Purchase;
using SCM.Domain;
using SCM.ErrorCode;
using SCM.Infrastructure.Implement;

namespace SCM.API.Write.Application.Handler.Purchase
{
    public class UpdPurchaseHandler : IRequestHandler<UpdPurchaseCommand, APIResult<PurchaseManagementModel>>
    {
        DatabaseContext db;

        public UpdPurchaseHandler(DatabaseContext db)
        {
            this.db = db;
        }

        public Task<APIResult<PurchaseManagementModel>> Handle(UpdPurchaseCommand request, CancellationToken cancellationToken)
        {
            APIResult<PurchaseManagementModel> res=new APIResult<PurchaseManagementModel>();
            var purchase= db.SqlSugar.Queryable<PurchaseManagementModel>().Where(s => s.Id == request.Id && s.IsDel == false).First();
            if(purchase==null)
            {
                res.Code=APIEnum.Fail;
                res.Msg = "不存在";
                return Task.FromResult(res);
            }
            res.Code=APIEnum.Success;
            res.Data = purchase;
            return Task.FromResult(res);
        }
    }
}
